package com.ruoyi.web.controller.zsix;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsix.domain.model.TaskManageParams;
import com.ruoyi.zsix.domain.process.ProcessTask;
import com.ruoyi.zsix.domain.taskAssignee.ApprovalTrack;
import com.ruoyi.zsix.domain.taskAssignee.GenerWork;
import com.ruoyi.zsix.domain.taskAssignee.ReturnWorkEntity;
import com.ruoyi.zsix.service.IApprovalTrackService;
import com.ruoyi.zsix.service.IProcessTaskService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/task")
public class ProcessTaskController  extends BaseController {
    @Autowired
    private IProcessTaskService processTaskService;
    @Autowired
    private IApprovalTrackService approvalTrackService;

    /**
     * 获取任务表单内容
     * @param generWork
     * @return
     */
    @Log(title = "获取任务表单内容" , businessType = BusinessType.EXPORT)
    @PostMapping("/getHisFormJson")
    public AjaxResult getHisFormJson(@RequestBody GenerWork generWork){
        return success(processTaskService.getHisFormJson(generWork));
    }
    @Log(title = "启动流程" , businessType = BusinessType.EXPORT)
    @PostMapping("/startProcess")
    public AjaxResult startProcess(@RequestBody ProcessTask processTask){
        try{
            return success(processTaskService.startProcess(processTask));
        }
        catch (Exception ex){
            return error(ex.toString());
        }
    }
    @Log(title = "发送流转" , businessType = BusinessType.EXPORT)
    @PostMapping("/sendWork")
    public AjaxResult sendWork(@RequestBody ProcessTask processTask){
        HashMap<String,Object> variables=processTask.getVariables();
        variables.put("isPass", "0");
        processTask.setVariables(variables);
        try{
            return success(processTaskService.sendWork(processTask));
        }
        catch (Exception ex){
            return error(ex.toString());
        }
    }

    /**
     * 驳回
     * @param returnWorkEntity
     * @return
     */
    @Log(title = "驳回" , businessType = BusinessType.EXPORT)
    @PostMapping("/returnWork")
    public AjaxResult returnWork(@RequestBody ReturnWorkEntity returnWorkEntity){
        return success(processTaskService.returnWork(returnWorkEntity));
    }

    /**
     * 工作移交
     * @param params
     * @return
     */
    @Log(title = "移交" , businessType = BusinessType.EXPORT)
    @PostMapping("/transfer")
    public AjaxResult transfer(@RequestBody TaskManageParams params){
        return success(processTaskService.transfer(params));
    }

    /**
     * 批量移交
     * @param params
     * @return
     */
    @Log(title = "批量移交" , businessType = BusinessType.EXPORT)
    @PostMapping("/transferBatch")
    public AjaxResult transferBatch(@RequestBody TaskManageParams params){
        return success(processTaskService.transferBatch(params));
    }

    /**
     * 加签
     * @param params
     * @return
     */
    @Log(title = "加签" , businessType = BusinessType.EXPORT)
    @PostMapping("/countersign")
    public AjaxResult countersign(@RequestBody TaskManageParams params){
        return success(processTaskService.countersign(params));
    }

    /**
     * 拒绝
     * @param params
     * @return
     */
    @Log(title = "拒绝" , businessType = BusinessType.EXPORT)
    @PostMapping("/refuse")
    public AjaxResult refuse(@RequestBody TaskManageParams params){
        return success(processTaskService.refuse(params));
    }

    /**
     * 结束流程
     * @param params
     * @return
     */
    @Log(title = "手动结束流程" , businessType = BusinessType.EXPORT)
    @PostMapping("/setEndTask")
    public AjaxResult setEndTask(@RequestBody TaskManageParams params){
        return success(processTaskService.setEndTask(params));
    }

    /**
     * 批量结束
     * @param params
     * @return
     */
    @Log(title = "批量结束流程" , businessType = BusinessType.EXPORT)
    @PostMapping("/setEndTaskBatch")
    public AjaxResult setEndTaskBatch(@RequestBody TaskManageParams params){
        return success(processTaskService.setEndTaskBatch(params));
    }

    /**
     * 删除流程
     * @param params
     * @return
     */
    @Log(title = "删除流程实例与任务" , businessType = BusinessType.EXPORT)
    @PostMapping("/deleteProcInst")
    public AjaxResult deleteProcInst(@RequestBody TaskManageParams params){
        return success(processTaskService.deleteProcInst(params));
    }
    @Log(title = "获取流程轨迹" , businessType = BusinessType.EXPORT)
    @PostMapping("/getUserTaskTrack")
    public AjaxResult getUserTaskTrack(@RequestBody ApprovalTrack approvalTrack){
        return success(approvalTrackService.getUserTaskTrack(approvalTrack));
    }
    @Log(title = "获取待审核列表" , businessType = BusinessType.EXPORT)
    @GetMapping("/getTodoList")
    public TableDataInfo getTodoList(@RequestParam("flowName") String flowName){
        startPage();
        List<GenerWork> list=processTaskService.getTodoList(flowName);
        return getDataTable(list);
    }
    @Log(title = "获取已审核列表" , businessType = BusinessType.EXPORT)
    @GetMapping("/getHistoricTaskInstance")
    public TableDataInfo getHistoricTaskInstance(@RequestParam("flowName") String flowName){
        startPage();
        List<GenerWork> list=processTaskService.getHistoricTaskInstance(flowName);
        return getDataTable(list);
    }

    /**
     * 获取审核按钮
     * @param taskid
     * @return
     */
    @Log(title = "获取审核按钮" , businessType = BusinessType.EXPORT)
    @GetMapping("/getElementButton")
    public AjaxResult getElementButton(@RequestParam("taskid") String taskid){
        return success(processTaskService.getElementButton(taskid));
    }

    /***
     * 获取轨迹图/流程图
     * @param httpServletResponse
     * @param processInstanceId
     * @param porcessKey
     */
    @Log(title = "获取轨迹图" , businessType = BusinessType.EXPORT)
    @GetMapping(value = "/getTrack")
    public void processImageGet(HttpServletResponse httpServletResponse,
                                @RequestParam("processInstanceId") String processInstanceId,
                                @RequestParam("porcessKey") String porcessKey) {
        try (OutputStream outputStream = httpServletResponse.getOutputStream()) {
            InputStream img = approvalTrackService.getFlowImgByProcInstId(processInstanceId,porcessKey);
            byte[] bytes = IOUtils.toByteArray(img);
            httpServletResponse.setContentType("image/svg+xml");
            outputStream.write(bytes);
            outputStream.flush();
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

    /**
     * 获取审核记录表
     */
    @Log(title = "获取审核记录" , businessType = BusinessType.EXPORT)
    @PostMapping(value = "/getApprovalTracks")
    public AjaxResult getApprovalTracks(@RequestBody ApprovalTrack approvalTrack) {
        return success(approvalTrackService.approvalTracks(approvalTrack));
    }
}
